﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.IssueNew.Enums;
using UFIDA.U9.IssueNew.MaterialDeliveryBP;
using UFIDA.U9.IssueNew.MaterialDeliveryDocBE;
using UFSoft.UBF.Business;

namespace ufida.u9.ait.openapi
{
    internal class MaterialDeliveryDocBase
    {
        public static MaterialDeliveryDoc SubmitDoc(MaterialDeliveryDoc doc)
        {
            if(doc.DocState != DocStateEnum.Opened)
            {
                throw new Exception("单据状态不为开立");
            }

            MaterialDeliveryApprove bp = new MaterialDeliveryApprove();
            bp.MaterialDeliveryDoc = doc.Key;
            bp.CurrentSysVersion = doc.SysVersion;
            bp.IsAutoApp = true;
            bp.IsSubmit = true;
            bp.Do();

            return doc;
        }

        public static MaterialDeliveryDoc ApproveDocEx(DocDTOData dto,MaterialDeliveryDoc doc)
        {
            if (!String.IsNullOrEmpty(dto.DocDate))
            {
                //修改日期
                using(ISession session = Session.Open())
                {
                    doc.BusinessDate= DateTime.Parse(dto.DocDate);
                    doc.ConfirmDate = doc.BusinessDate;
                    session.Commit();
                }
            }
            return ApproveDoc(doc);
        }
        public static MaterialDeliveryDoc ApproveDoc(MaterialDeliveryDoc doc)
        {
            if(doc.DocState == DocStateEnum.Approved || doc.DocState == DocStateEnum.Closed)
            {
                throw new Exception("单据状态为已审核或关闭");
            }
            if(doc.DocState == DocStateEnum.Opened)
            {
                SubmitDoc(doc);
                doc = doc.Key.GetEntity();
            }

            MaterialDeliveryApprove bp = new MaterialDeliveryApprove();
            bp.MaterialDeliveryDoc = doc.Key;
            bp.CurrentSysVersion = doc.SysVersion;
            bp.IsAutoApp = true;
            bp.IsSubmit = false;
            bp.Do();

            return doc;
        }

        public static MaterialDeliveryDoc UnApproveDoc(MaterialDeliveryDoc doc)
        {
            if (doc.DocState != DocStateEnum.Approved)
            {
                throw new Exception("单据状态不为已审核");
            }

            MaterialDeliveryUnApprove bp = new MaterialDeliveryUnApprove();
            bp.MaterialDeliveryDoc = doc.Key;
            bp.CurrentSysVersion = doc.SysVersion;
            bp.Do();

            return doc;
        }
    }
}
